from django.contrib import admin
from apps.base.models import *
from apps.source.models import SupplierPicture
import re



@admin.register(ProductSeries)
class ProductSeriesAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'first')


# 产品资源库
@admin.register(ProductSource)
class ProductSourceAdmin(admin.ModelAdmin):
    list_display = (
    'A001', 'F002', 'D001', 'D002', 'B001', 'B002', 'B003', 'B004', 'B005', 'B006', 'get_viscosity_range')
    list_filter = ('B001', 'B002', 'B003', 'B004', 'B005', 'B006',)
    list_per_page = 30
    exclude = ('A001',)
    search_fields = ('D001', 'D002__A001', 'F002__B001')

    # 重写添加模型
    def save_model(self, request, obj, form, change):
        # 新添加时生成编号,以及更改对外型号
        if not obj.A001:
            obj.addId()
        if not obj.D002:
            obj.classify()
        return super().save_model(request, obj, form, change)


# 产品对外型号与资源库一起显示
class ProductSourceInline(admin.TabularInline):
    model = ProductSource


@admin.register(ProductName)
class ProductNameAdmin(admin.ModelAdmin):
    inlines = [ProductSourceInline, ]
    list_display = ('A001', 'F001', 'B001', 'B002', 'B003', 'B004', 'B005', 'B006', 'viscosity')
    list_filter = ('B001', 'B002', 'B003', 'B004', 'B005', 'B006',)
    list_per_page = 30
    search_fields = ('A001',)


@admin.register(Option)
class OptionAdmin(admin.ModelAdmin):
    list_display = ('value', 'selfkey')


admin.site.register(Tag)
